package com.jixiantubu.jinnang;

import image.SmartImageView;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.example.jixiantubu.R;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jixiantubu.adapter.YouJiMuLuMuAdapter;
import com.jixiantubu.bean.Travelsbean;
import com.jixiantubu.util.CustomProgressDialog;
import com.jixiantubu.util.MyApplication;
import com.jixiantubu.util.NoScrollListView;
import com.jixiantubu.util.ReboundScrollView;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class JinNang_YouJi_MuLu extends Activity implements OnClickListener {
	List<Travelsbean> list = new ArrayList<Travelsbean>();
	NoScrollListView listView;
	ReboundScrollView scrollView;
	YouJiMuLuMuAdapter adapter;
	ImageView goback, find;;
	SmartImageView userimage;
	TextView user_nickname;
    BitmapUtils bitmapUtils;
 
    private String url="http://" + MyApplication.getIP()
    + ":8080/Jinnang/KePuTuiJian_Middle";
    
    String url1="http://" + MyApplication.getIP()
			+ ":8080/Jinnang/img/";
    
    Travelsbean travelsbean;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Window window = this.getWindow();
		window.requestFeature(window.FEATURE_NO_TITLE);
		setContentView(R.layout.jinnang_youji_mulu);
		ViewUtils.inject(this);
		bitmapUtils = new BitmapUtils(this);
		initViews();
		initfind();
		initData();
	}

	private void initfind() {
		user_nickname.setText(travelsbean.getUser_nickname());
		String userimageurl=url1+travelsbean.getUser_imgPath();
		userimage.setstyle("circle");
		userimage.setImageUrl(userimageurl, R.drawable.error, R.drawable.loding);
	}

	private void initViews() {
		Intent intent=getIntent();
		travelsbean=(Travelsbean) intent.getSerializableExtra("mulu");
	Log.e("123321胡蛟龙", travelsbean.toString());
		
		
		listView = (NoScrollListView) findViewById(R.id.JinNang_YouJi_MuLu_lv);
		listView.setDivider(null);
		scrollView = (ReboundScrollView) findViewById(R.id.jinnang_youji_mulu_scrollview);
		scrollView.requestChildFocus(listView, null);
		
		userimage = (SmartImageView) findViewById(R.id.JinNang_YouJi_MuLu_handimage);
		user_nickname = (TextView) findViewById(R.id.JinNang_YouJi_MuLu_user_nickname);
		
		goback = (ImageView) findViewById(R.id.JinNang_youji_MuLu_back);
		find = (ImageView) findViewById(R.id.JinNang_YouJi_MuLu_find);
		goback.setOnClickListener(this);
		find.setOnClickListener(this);
		//userimage.setOnClickListener(this);
	}
	public void setListViewHeightBasedOnChildren(ListView listView) {
		
		 ListAdapter listAdapter = listView.getAdapter();
		 if (listAdapter == null) {
		 return;
		 }	
		 int totalHeight = 0;
		 for (int i = 0, len = listAdapter.getCount(); i < len; i++) {	 
		 View listItem = listAdapter.getView(i, null, listView);
		 listItem.measure(0, 0);
		 totalHeight += listItem.getMeasuredHeight();
		 }
		
		 ViewGroup.LayoutParams params = listView.getLayoutParams();
		 params.height = totalHeight+ (listView.getDividerHeight() *
		 (listAdapter.getCount() - 1));
		 listView.setLayoutParams(params);
		 }
	private void initData() {
		adapter =new YouJiMuLuMuAdapter(JinNang_YouJi_MuLu.this, list);
		listView.setAdapter(adapter);
		setListViewHeightBasedOnChildren(listView);
		final CustomProgressDialog pd = new CustomProgressDialog(
				JinNang_YouJi_MuLu.this, "正在拼命加载数据,请稍等！", R.anim.frame);
		pd.show();
		RequestParams params=new RequestParams();
		params.addBodyParameter("key", "4");
		params.addQueryStringParameter("page", "1");
		params.addQueryStringParameter("user_id",String.valueOf(travelsbean.getUser_id()));
		HttpUtils httpUtils=new HttpUtils();
		MyApplication.httpclient.configSoTimeout(5000);
		httpUtils.send(HttpMethod.POST, url, params, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				pd.dismiss();
				MyApplication.show(JinNang_YouJi_MuLu.this, "加载数据失败，请检查网络!");
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				pd.dismiss();
				ArrayList<Travelsbean> templist;
				String result=arg0.result;
				Gson gson=new Gson();
				Type type = new TypeToken<List<Travelsbean>>() {
				}.getType();
				templist=gson.fromJson(result, type);
				list.addAll(templist);
				list=gson.fromJson(result, type);
				adapter.setList(list);
				Log.e("下大雨", list.toString());
				
				
				adapter.notifyDataSetChanged();
			}
		});
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.JinNang_youji_MuLu_back:
			finish();
			break;
		case R.id.JinNang_YouJi_MuLu_find:
			Intent intent1 = new Intent(JinNang_YouJi_MuLu.this,
					JinNang_YouJi_Find.class);
			startActivity(intent1);
			break;
		/*case R.id.JinNang_YouJi_MuLu_handimage:
Intent intent=new Intent(JinNang_YouJi_MuLu.this,);
			break;*/
		case R.id.JinNang_YouJi_MuLu_lv:
			Intent intent=new Intent(JinNang_YouJi_MuLu.this,
					JinNang_YouJi_JuTiNeiRong.class);
			startActivity(intent);
						break;
		default:
			break;
		}

	}

}
